import Router from '@koa/router';
import response from '../response.js';
import { qipiao_get, qipiao_post } from '../qipiao.js';
import db from '../mysqlHelper.js';
import { createToken } from '../auth.js';
import moment from 'moment';

const router = new Router();


async function voucherRecharge(isp_code, value, expire_time,oper_name) {

    if (expire_time == null || expire_time == undefined) {
        expire_time = moment().add(10, 'year').format('YYYY-MM-DD HH:mm:ss');
    }
    let recharge_record = {
        isp_code: isp_code,
        oper_name:oper_name,
        recharge_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        oper_type: 1,//人工充值
        expire_time: expire_time,
        value: value
    }
    let change_record = {
        isp_code: isp_code,
        change_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        balance: 0,
        change_value: value,
        inventory: value,
        oper_type: 2//充值
    }


    let stock = {
        isp_code: isp_code,
        inventory: value,
        creat_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        expire_time: expire_time,
        oper_name:oper_name
    }
    //采用事务写入数据库
    let sqls = [];
    sqls.push({ sql: 'insert into isp_voucher_recharge_record set ?', param: recharge_record });
    sqls.push({ sql: 'insert into isp_voucher_change_record set ?', param: change_record });
    sqls.push({ sql: 'insert into isp_voucher_stock set ?', param: stock });
    let res = await db.transaction(sqls);
    console.log('充值结果', res);
    if (res instanceof Error) {
        return { err: '数据库写入错误:' + res.message };
    }
    return { code: 1 };

}
//服务商代金券充值接口
router.post('/api/voucher/recharge', async (ctx) => {
    let body = ctx.request.body;
    // if(ctx.state.oper==null){
    //     ctx.body = response.fail('请使用操作员Token调用该接口');
    //     return;
    // }
    if (body.isp_code == null || body.isp_code == undefined) {
        ctx.body = response.fail('isp_code不能为空');
        return;
    }
    if (body.value == null || body.value == undefined) {
        ctx.body = response.fail('value不能为空');
        return;
    }
    if (body.expire_time == null || body.expire_time == undefined) {
        ctx.body = response.fail('expire_time不能为空');
        return;
    }
    let oper_name= ctx.state.oper.name;
    let res = await voucherRecharge(body.isp_code, body.value, body.expire_time,oper_name);
    console.log('充值返回', res);
    if (res.err) {
        ctx.body = response.fail(res.err);
        return;
    }
    ctx.body = response.success('充值成功');

});

router.post('/api/voucher/get', async (ctx) => {
    let body = ctx.request.body;
    if(body.phone==undefined &&  body.phone==undefined){
        ctx.body = response.fail('phone 或者opeid不能同时为空');
        return;
    }
    let sql=''
    if(body.phone==undefined){
        sql=`select sum(num) as total_voucher from user_voucher where  phone='${body.phone}' and expire_time>'${moment().format('yyyy-MM-DD HH:mm:ss')}' and num>0`;
    }else{
        sql=`select sum(num) as total_voucher from user_voucher where  openid='${body.openid}' and expire_time>'${moment().format('yyyy-MM-DD HH:mm:ss')}' and num>0`;
    }
    let res = await db.query(sql);
    if (res instanceof Error) {
        ctx.body = response.fail('查询失败',res.message);
        return;
    }
    ctx.body = response.success(res);
});


export { router };
